//------------------------------------------------------------------------
// Abstract: Define of class Memory
//
// Authoring: XiaoHanxiong
//
// Time: 2013.12.30
//
// Static test:
//
// Version history:
//		Ver_1.0 : 2013.12.30	XiaoHanxiong
//------------------------------------------------------------------------
#ifndef _Library_Memory_h_
#define _Library_Memory_h_

#include "Stdafx.h"

namespace Library
{
	class _LIBRARY Memory
	{
	public:
		// Constructor
		Memory();
		// Copy Constructor
		Memory(const Memory& memory);
		// Destructor
		~Memory();
	
	/* Public Operation */
	public:
		// Push back a number of bytes
		void PushBack(UINT8 value);
		void PushBack(UINT16 value);
		void PushBack(UINT32 value);
		void PushBack(float value);
		void PushBack(double value);
		void PushBack(void* value, size_t size);
		// Insert front a number of bytes
		void PushFront(UINT8 value);
		void PushFront(UINT16 value);
		void PushFront(UINT32 value);
		void PushFront(float value);
		void PushFront(double value);
		void PushFront(void* value, size_t size);
	public:
		// Get the specified byte value by index
		UINT8 Get(size_t index);
		// Set the specified byte value by index
		void Set(size_t index, UINT8 value);
		// Get the data size
		size_t Size();
		// Increase the memory capcity size
		void Increase(size_t front, size_t back);
		// Get the whole memory capacity
		size_t Capacity();
		// Get the head address of data
		const byte* Data();
		// Clear the memory
		void Clear();
		// Return the begin of data
		const byte* Begin();
		// Return the end of data
		const byte* End();
		// Return the middle between begin and end of data
		const byte* Zero();

	public:
		// Combine two memory object data together
		Memory& operator += (Memory& memory);
		// Copy memory object data to anther
		Memory& operator = (Memory& memory);
		// Get the specified byte value by index
		UINT8 operator [] (size_t index);

	/* Private operation*/
	protected:
		// Initialize data member
		void inline Initialize();
		// Release memory and reset data member
		void inline Release();
		// Allocate memory
		void Allocate(size_t front, size_t back);
		// Make a copy of the specified memory object 
		void Copy(const Memory& memory);
	protected:
		// Point to the head address of memory
		byte* m_base;
		// Point to the address of memory which divide front and back
		byte* m_zero;
		// Point to the value begin address of memory
		byte* m_front;
		// Point to the value end address of memory
		byte* m_back;
		// The valid size of memory before zero
		size_t m_front_size;
		// The valid size of memory after zero
		size_t m_back_size;
		// The first allocated memory size(16 bytes)
		static const size_t MCS_BASIC_ALLOCATION = 0x10u;
	};
}// _Library_

#endif